<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var reverseOnlyLetters = function (S) {
      let rules = /[a-zA-Z]/
      // 存储需要逆转的数组
      let arr = []
      // 原数组
      let curArr = S.split("")
      for (let i = 0; i < curArr.length; i++) {
        if (rules.test(curArr[i])) {
          arr.push(curArr[i])
        }
      }
      // 替换插入 
      for (let i = 0; i < curArr.length; i++) {
        if (rules.test(curArr[i])) {
          curArr[i] = arr.pop()
        }
      }
      return curArr.join("")
    };
    console.log(reverseOnlyLetters(s = "Test1ng-Leet=code-Q!"))
  </script>
</body>

</html>